
package mapper;

import Utilities.DBConnection;
import dto.DTO_SanPham;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MAP_SanPham {
    public ArrayList<DTO_SanPham> dsLoaiSanPham(int ID,int gt) throws Exception
    {
        ArrayList<DTO_SanPham> dsLoaiSanPham = new ArrayList<DTO_SanPham>();
        DTO_SanPham sp = null;
        try
        {
             String qry = "select * from SanPham where LoaiSP = "+ID + " and GioiTinh = "+gt;
             ResultSet rs = DBConnection.getResultSet(qry);
             while(rs!=null && rs.next())
             {
                    sp = new DTO_SanPham();
                    initSanPham(sp, rs);
                    dsLoaiSanPham.add(sp);
             }


        }
        catch(Exception e)
        {
            throw e;

        }
        return dsLoaiSanPham;
    }
    public ArrayList<DTO_SanPham> dsChuDeSP(int ID)
    {
            ArrayList<DTO_SanPham> dsChuDeSP = new ArrayList<DTO_SanPham>();
            DTO_SanPham sp = null;
            try {
                String qry = "select * from SanPham where ChuDe = "+ID;
                ResultSet rs = DBConnection.getResultSet(qry);
                while(rs!=null && rs.next())
                {
                    sp = new DTO_SanPham();
                    initSanPham(sp, rs);
                    dsChuDeSP.add(sp);
                }

                } catch (SQLException ex) {
                    Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null, ex);
                }
            return dsChuDeSP;
    }
    public void initSanPham(DTO_SanPham sp ,ResultSet rs)
    {
        try {
            sp.setMaSP(rs.getInt(1));
            sp.setTenSP(rs.getString(2));
            sp.setGiaBan(Integer.parseInt(rs.getString(3)));
            sp.setGiaMua(Integer.parseInt(rs.getString(4)));
            sp.setLoaiSP(Integer.parseInt(rs.getString(5)));
            sp.setChuDe(Integer.parseInt(rs.getString(6)));
            sp.setThongTin(rs.getString(7));
            sp.setGioiTinh(Integer.parseInt(rs.getString(8)));
            sp.setHinhAnh(rs.getString(9));
        } catch (SQLException ex) {
            Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public DTO_SanPham LayThongTinSanPham(int MaSP)
    {
        DTO_SanPham dtoSP = new DTO_SanPham();
        try {
            String qry = "select * from SanPham where MaSP = " + MaSP;
            ResultSet rs = DBConnection.getResultSet(qry);
            if (rs != null && rs.next()) {
                initSanPham(dtoSP, rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dtoSP;
    }
    public ArrayList<DTO_SanPham> LoadDSSP()
    {
        ArrayList<DTO_SanPham> dssp = new ArrayList<DTO_SanPham>();
        DTO_SanPham sp = null;
        try{
            String qry = "select * from SanPham";
            ResultSet rs = DBConnection.getResultSet(qry);
            while(rs!=null && rs.next())
                {
                    sp = new DTO_SanPham();
                    initSanPham(sp, rs);
                    dssp.add(sp);
                }
        }
        catch(SQLException e){
            Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null,e);
        }
        return dssp;
    }
    public ArrayList<DTO_SanPham> LoadDSSPTheoMaSP(int masp)
    {
        ArrayList<DTO_SanPham> dssp = new ArrayList<DTO_SanPham>();
        DTO_SanPham sp = null;
        try{
            String qry = "select * from SanPham where MaSP ="+masp;
            ResultSet rs = DBConnection.getResultSet(qry);
            while(rs!=null && rs.next())
                {
                    sp = new DTO_SanPham();
                    initSanPham(sp, rs);
                    dssp.add(sp);
                }
        }
        catch(SQLException e){
            Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null,e);
        }
        return dssp;
    }
    public ArrayList<DTO_SanPham> LoadDSSPTheoTenSP(String tensp)
    {
        ArrayList<DTO_SanPham> dssp = new ArrayList<DTO_SanPham>();
        DTO_SanPham sp = null;
        try{
            String qry = "select * from SanPham where TenSP LIKE '%"+tensp+"%'";
            ResultSet rs = DBConnection.getResultSet(qry);
            while(rs!=null && rs.next())
                {
                    sp = new DTO_SanPham();
                    initSanPham(sp, rs);
                    dssp.add(sp);
                }
        }
        catch(SQLException e){
            Logger.getLogger(MAP_SanPham.class.getName()).log(Level.SEVERE, null,e);
        }
        return dssp;
    }
   
     public boolean  suasanpham(DTO_SanPham sp)
    {
         String qry="Update SanPham set TenSP= '"+sp.getTenSP()+
                 "',GiaMua ='"+sp.getGiaMua()+"',GiaBan='"+sp.getGiaBan()+
                 "',LoaiSP='"+sp.getLoaiSP()+"',ChuDe='"+sp.getChuDe()+
                 "',ThongTin='"+sp.getThongTin()+"',GioiTinh='"+sp.getGioiTinh()+
                 "',HinhAnh='"+sp.getHinhAnh()+
                 "' Where MaSP = "+sp.getMaSP();
                 int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }
    }
    public boolean  themsanpham(DTO_SanPham sp)
    {
        String qry="Insert into SanPham(TenSP,GiaMua,GiaBan,LoaiSP,ChuDe,ThongTin,GioiTinh,HinhAnh) Values('"+sp.getTenSP()+
                 "','"+sp.getGiaMua()+"','"+sp.getGiaBan()+
                 "','"+sp.getLoaiSP()+"','"+sp.getChuDe()+
                 "','"+sp.getThongTin()+"','"+sp.getGioiTinh()+
                 "','"+sp.getHinhAnh()+"')";
                 int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }
    }
     public boolean  xoasanpham(int masp)
    {
        String qry="Delete SanPham where MaSP ="+masp;
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }
    }
}
